package org.jeecg.modules.pdd.api;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.PopHttpClient;
import com.pdd.pop.sdk.http.api.pop.request.PddDdkGoodsPromotionUrlGenerateRequest;
import com.pdd.pop.sdk.http.api.pop.request.PddDdkGoodsSearchRequest;
import com.pdd.pop.sdk.http.api.pop.response.PddDdkGoodsPromotionUrlGenerateResponse;
import com.taobao.api.ApiException;
import com.taobao.api.request.TbkDgMaterialOptionalRequest;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkItemInfoGetResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.mall.BeanMapperUtil;
import org.jeecg.common.util.mall.HttpUtils;
import org.jeecg.modules.mall.entity.*;
import org.jeecg.modules.mall.service.*;
import org.jeecg.modules.mall.service.impl.Contents;
import org.jeecg.modules.pdd.entity.PddGoods;
import org.jeecg.modules.pdd.entity.PddGoodsCats;
import org.jeecg.modules.pdd.entity.PddGoodsRecommend;
import org.jeecg.modules.pdd.entity.PddPromotion;
import org.jeecg.modules.pdd.service.IPddGoodsCatsService;
import org.jeecg.modules.pdd.service.IPddGoodsRecommendService;
import org.jeecg.modules.pdd.service.IPddGoodsService;
import org.jeecg.modules.pdd.service.IPddPromotionService;
import org.jeecg.modules.pdd.util.DingDanXiaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 描述
 * </p>
 */
@Slf4j
@Api(tags = "app拼多多")
@RestController
@RequestMapping("/app/pdd/api")
public class PinDuoDuoController {

    @Autowired
    private IPddGoodsCatsService pddGoodsCatsService;

    @Autowired
    private IPddGoodsService pddGoodsService;

    @Autowired
    private IPddGoodsRecommendService pddGoodsRecommendService;

    @Autowired
    private IPddPromotionService pddPromotionService;

    @AutoLog(value = "获取类目元素")
    @ApiOperation(value = "获取类目元素", notes = "获取类目元素")
    @GetMapping(value = "/classify/getCats")
    public Result<?> getCats() {
        log.info("获取类目元素列表");
        QueryWrapper<PddGoodsCats> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("cat_id");
        return Result.ok(pddGoodsCatsService.list(queryWrapper));
    }

    @AutoLog(value = "PDD获取首页商品列表")
    @ApiOperation(value = "PDD获取首页商品列表", notes = "获取首页商品列表")
    @GetMapping(value = "/index/getProductList")
    public Map<String, Object> getPageList(PddGoodsCats pddGoodsCats, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                           HttpServletRequest req) {
        Map<String, Object> resultMap = new HashMap<>();
        if(null==pddGoodsCats||pddGoodsCats.getCatId()==null){
            resultMap.put("state", false);
            resultMap.put("msg", "CatId参数错误");
            return resultMap;
        }
        log.info("PDD获取首页商品列表,dat:{}", DateUtil.currentSeconds());
        String jsonData = DingDanXiaUtil.getPddGoodsByCatId(pddGoodsCats.getCatId(), pageNo, pageSize);
        resultMap.put("state", true);
        resultMap.put("data", jsonData);
        log.info("PDD获取首页商品列表,dat:{}", DateUtil.currentSeconds());
        return resultMap;
    }

//
//    @AutoLog(value = "获取拼多多推荐商品")
//    @ApiOperation(value = "获取拼多多推荐商品")
//    @GetMapping(value = "/index/getPddGoodsRecommend")
//    public Result<?> getPddGoodsRecommend(PddGoodsRecommend pddGoodsRecommend, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
//                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
//                                          HttpServletRequest req) {
//
//        return pddGoodsRecommendService.getPddGoodsRecommend(pddGoodsRecommend, pageNo, pageSize, req);
//    }

    @AutoLog(value = "获取拼多多推荐商品")
    @ApiOperation(value = "获取拼多多推荐商品")
    @GetMapping(value = "/index/getPddGoodsRecommend_new")
    public Map<String, Object> getPddGoodsRecommend_new(String user_id, String list_id, Integer limit, Integer sort_type, Integer offset) {
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isEmpty(user_id)) {
            resultMap.put("state", false);
            resultMap.put("msg", "user_id参数错误");
            return resultMap;
        }
        QueryWrapper pddqueryWrapper = new QueryWrapper();
        pddqueryWrapper.eq("user_id", user_id);
        PddPromotion pddPromotion = pddPromotionService.getOne(pddqueryWrapper);
        if (null == pddPromotion) {
            resultMap.put("state", false);
            resultMap.put("msg", "用户数据错误");
            return resultMap;
        }
        String jsonData = DingDanXiaUtil.getPddGoodsData(pddPromotion.getPid(), list_id, limit, sort_type, offset);
        resultMap.put("state", true);
        resultMap.put("data", jsonData);
        return resultMap;
    }


    /**
     * 超级搜索产品列表
     */
    @AutoLog(value = "超级搜索产品列表")
    @ApiOperation(value = "超级搜索产品列表", notes = "超级搜索产品列表")
    @PostMapping(value = "/query/superQueryList")
    public Result<?> superQueryList(String keyword, Integer pageSize, Integer page, Integer sort_type, HttpServletRequest request) {
        log.info("超级搜索产品列表,optionalDo:{}", request.toString());
        List<PddGoods> pageList = null;
        try {
            pageList = pddGoodsService.getSuperQueryList(keyword, pageSize, page, sort_type, request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        log.info("超级搜索产品列表,optionalDo:{}", request.toString());
        return Result.ok(pageList);
    }

    @AutoLog(value = "获取商品详情")
    @ApiOperation(value = "获取商品详情", notes = "获取商品详情")
    @GetMapping(value = "/detail/getProductDetail")
    public Result<?> getProductDetail(PddGoods pddGoods) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            log.info("开始跳转到商品详情,goodsid:{},date:{}", pddGoods.getGoodsId(), DateUtil.currentSeconds());
            String jsonData = pddGoodsService.getProductDetail(pddGoods);
            resultMap.put("productdetail", jsonData);
            resultMap.put("state", 1);
            log.info("结束跳转到商品详情,goodsid:{},date:{}", pddGoods.getGoodsId(), DateUtil.currentSeconds());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
        return Result.ok(resultMap);
    }

    @AutoLog(value = "跳转拼多多接口")
    @ApiOperation(value = "跳转拼多多接口", notes = "跳转拼多多接口")
    @GetMapping(value = "/detail/openPddDetail")
    public Result<?> openPddDetail(Long goodsId, String user_id) throws Exception {
        if (null == goodsId) {
            return Result.error("参数错误");
        }
        return pddGoodsService.openPddDetail(goodsId, user_id);
    }
}
